﻿@model Sysgeprava.Entities.UBIGEO

@{
    ViewBag.Title = "NUEVO";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<link href="@Url.Content("~/Content/desing.css")" rel="stylesheet" />

@{
    var message = TempData["AlertMessage"] ?? string.Empty;
}

<script type="text/javascript">
    var message = '@message';
    if (message) {
        alert(message).alert;
    }
</script>

<div class="float-center" id="panel_titulo">
   <img src="~/Images/titulos/mttoUbigeo.png" alt="mttoUbigeo" height="70" width="400"/>
</div>


<h2></h2>


@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>UBIGEO</legend>

        <div id="general" style="width:330px;">
            <b>Código: </b>
            <div class="editor-field">
                @Html.Editor("CodUbigeo")
                @Html.ValidationMessageFor(model => model.CodUbigeo)
            </div>

            <b>Departamento: </b>
            <div class="editor-field">
                @Html.EditorFor(model => model.Departamento)
                @Html.ValidationMessageFor(model => model.Departamento)
            </div>

            <b>Provincia: </b>
            <div class="editor-field">
                @Html.EditorFor(model => model.Provincia)
                @Html.ValidationMessageFor(model => model.Provincia)
            </div>

            <b>Distrito: </b>
            <div class="editor-field">
                @Html.EditorFor(model => model.Distrito)
                @Html.ValidationMessageFor(model => model.Distrito)
            </div>
        </div>

        <p>
            <button type="submit" name="btnGuardar" id="btnGuardar" value="Create"><img src="~/Images/botones/save.png"></button>
            <button onclick="location.href='@Url.Action("Index")';return false;" ><img src="~/Images/botones/return.png"></button>
        </p>
    </fieldset>
}

@*<div>
    @Html.ActionLink("Back to List", "Index")
</div>*@

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        
        $("#CodUbigeo").focusout(function () {
            var codigo = $(this).val();
            var IdUbigeo = $("#Id").val();
            var div = $(this).closest("div");

            if (codigo.length == 6) {
                $.ajax({
                    url: "/Ubigeo/ValidarUbigeo?codigo=" + codigo + "&id=" + IdUbigeo,
                    dataType: "json",
                    success: function (data) {
                        if (data.existe == true) {
                            $(div).find("spam").remove();
                            alert("Ubigeo ya se encuentra Registrado");
                            $(div).append('<spam class="field-validation-error"><br/>Ubigeo: ' + data.Ubigeo + '</spam>');
                            $("#btnGuardar").hide();
                            $("#CodUbigeo").focus();
                        }
                        else {
                            $(div).find("spam").remove();
                            $("#btnGuardar").show();
                        }
                    }
                });
            }
            else {
                alert("Longitud de CODIGO INCORRECTA");
                $("#CodUbigeo").focus();
            }
        });
    </script>
}
